//
//  MainMenuLayer.m
//
//  This is the source file for the main menu scene. It shows
//  the main menu of the game and it presents several options
//  through menu item buttons
//

#import "MainMenuLayer.h"
#import "LevelLayer.h"
#import "OptionsLayer.h"
#import "CreditsLayer.h"
#import "HighScoresLayer.h"
#import "HelpLayer.h"
#import "SimpleAudioEngine.h"
#import "GameState.h"

static const int kGameTitleSize = 104;
static const int EffectsLevel = 0;

@implementation MainMenuLayer

// returns a scene object containing the current layer
// this essentially loads the "screen" for a
// particular section or part of the game
+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    MainMenuLayer *mainMenu = [MainMenuLayer node];
    [scene addChild: mainMenu];
    
    return scene;
}

// standard iOS popup that functions as a stub
// for some of the buttons that haven't been
// implemented yet
-(void) popup
{
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Hello There!"];
    [dialog setMessage:@"You clicked one of the buttons!"];
    [dialog addButtonWithTitle:@"Yes"];
    [dialog addButtonWithTitle:@"No"];
    [dialog show];
    [dialog release];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

// this function is called when the player presses the start
// game button. it immediately loads the first level of the game
-(void) playPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                        transitionWithDuration:0.3 scene:[LevelLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(void) optionsPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                        transitionWithDuration:0.3 scene:[OptionsLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(void) highScoresPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                        transitionWithDuration:0.3 scene:[HighScoresLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(void) CreditsPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                                               transitionWithDuration:0.3 scene:[CreditsLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

-(void) HelpPressed
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade
                                               transitionWithDuration:0.3 scene:[HelpLayer scene]]];
    [[SimpleAudioEngine sharedEngine] playEffect:@"Menu_Click.wav"];
}

// initialize the main menu layer and display all the main menu buttons
-(id) init
{
    if ((self = [super init])) {
        CCMenuItemImage *playBtn = [CCMenuItemImage itemWithNormalImage:@"startGameButton.png"
            selectedImage:@"startGameButtonHighlight.png" target:self selector:@selector(playPressed)];
        
        CCMenuItemImage *highScoresBtn = [CCMenuItemImage itemWithNormalImage:@"highScoresButton.png"
            selectedImage:@"highScoresButtonHighlight.png" target:self selector:@selector(highScoresPressed)];
        
        CCMenuItemImage *optionsBtn = [CCMenuItemImage itemWithNormalImage:@"optionsButton.png"
            selectedImage:@"optionsButtonHighlight.png" target:self selector:@selector(optionsPressed)];
       
        CCMenuItemImage *creditsBtn = [CCMenuItemImage itemWithNormalImage:@"helpButton.png"
            selectedImage:@"helpButtonHighlight.png" target:self selector:@selector(HelpPressed)];
      
        CCMenuItemImage *helpBtn = [CCMenuItemImage itemWithNormalImage:@"creditsButton.png"
            selectedImage:@"creditsButtonHighlight.png" target:self selector:@selector(CreditsPressed)];
        
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        CCSprite *bg = [CCSprite spriteWithFile:@"menuBackground.png"];
        bg.position = ccp(winSize.width / 2, winSize.height / 2);
        [self addChild:bg z:0];
        
        CCMenu *mainMenu = [CCMenu menuWithItems:playBtn, highScoresBtn, optionsBtn, helpBtn, creditsBtn, nil];
        [mainMenu alignItemsVerticallyWithPadding:30.0];
        mainMenu.position = ccp(mainMenu.position.x, 300);
        [self addChild:mainMenu];
        
        CCLabelTTF *gameTitleLbl = [CCLabelTTF labelWithString:@"Inspection!" fontName:@"Marker Felt" fontSize:kGameTitleSize];
        gameTitleLbl.position = ccp(winSize.width / 2, winSize.height - kGameTitleSize);
        gameTitleLbl.color = ccc3(255, 255, 255);
        [self addChild:gameTitleLbl];

        CCLabelTTF *versionLbl = [CCLabelTTF labelWithString:@"v0.2 alpha" fontName:@"Marker Felt" fontSize:24];
        versionLbl.position = ccp(winSize.width - 64, 12);
        versionLbl.color = ccc3(255, 0, 120);
        [self addChild:versionLbl];
        
        if ([[[GameState sharedGameState] getState:@"music"] boolValue]) {
            if (![[SimpleAudioEngine sharedEngine] isBackgroundMusicPlaying]) {
                [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"backgroundMusic.mp3" loop:YES];
                [[SimpleAudioEngine sharedEngine]setBackgroundMusicVolume:0.4];
            }
        }
        
        if ([[[GameState sharedGameState] getState:@"sound"] boolValue] == 0) {
            [SimpleAudioEngine sharedEngine].effectsVolume = EffectsLevel;
        }
    }
    
    return self;
}

@end
